var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// 引入数据库连接
require('./database/database');


const cors = require('cors');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var authRouter = require('./routes/auth');
var AIRouter = require('./routes/AIjianyi');
const { expressjwt: expressJWT } = require('express-jwt');
const secret = "123456"
var app = express();
// const cors = require('cors');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(cors({
  origin: ['http://localhost:10086', 'https://localhost:10086', 'https://172.20.10.4'],
  credentials: true
}));
app.use(
  expressJWT({
    secret: secret,//创建token时自定义的密钥
    algorithms: ['HS256'],//加密规则
  }).unless({
    // path: ['/auth/login',"/auth/register","/auth/faceLogin", "/users","/ai/aiadd"]//白名单 访问这个路由的时候不需要进行验证
    path: [
      /^\/auth\/login/,
      /^\/auth\/register/,
      /^\/auth\/faceLogin/,
      /^\/auth\/update/,
      /^\/users/,
      /^\/api\/data/,
      /^\/api\/chat\/history/,
      /^\/api\/users/,
      /^\/api\/doctor/,
      '/auth/login',
      "/auth/register",
      "/auth/faceLogin", 
      "/users","/ai/aiadd",
      
    ]//白名单 访问这些路由的时候不需要进行验证
  })
)
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

// app.use(cors())
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/auth', authRouter);
app.use('/ai',AIRouter)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
